
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"

#define ESP_INTR_FLAG_DEFAULT 0

#define INPUT_TEST      0
#define OUTPUT_TEST     1
#define GPIO_TEST_MODE OUTPUT_TEST

uint8_t gpio_mux[]={46,45,42,41,40,16,15,37,36,35,34,33,48,21,20,19,18,17,39,38,2,7,12,6,10,5,8,14,13,11,4,3,9,1,0};


static xQueueHandle gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
    uint32_t gpio_num = (uint32_t) arg;
    xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}

static void gpio_task_example(void* arg)
{
    uint32_t io_num;
    for(;;) {
        if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
            printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
        }
    }
}

void app_main(void)
{
    while (1)
    {
#if (GPIO_TEST_MODE == OUTPUT_TEST)            
        for(int i=0;i<sizeof(gpio_mux);i++)
        {
            // int i=0;
            // gpio_mux[i]=44;
            gpio_config_t io_conf = {};
            //disable interrupt
            io_conf.intr_type = GPIO_INTR_DISABLE;
            //set as output mode
            io_conf.mode = GPIO_MODE_OUTPUT;
            //bit mask of the pins that you want to set,e.g.GPIO18/19
            io_conf.pin_bit_mask = 1ULL<<gpio_mux[i];
            //disable pull-down mode
            io_conf.pull_down_en = 0;
            //disable pull-up mode
            io_conf.pull_up_en = 0;
            //configure GPIO with the given settings
            gpio_config(&io_conf);               
            gpio_set_level(gpio_mux[i], 1);
            printf("gpio%d:1\r\n",gpio_mux[i]);
            vTaskDelay(3000 / portTICK_RATE_MS);
            gpio_set_level(gpio_mux[i], 0);
            printf("gpio%d:0\r\n",gpio_mux[i]);
            vTaskDelay(3000 / portTICK_RATE_MS);
            gpio_reset_pin(gpio_mux[i]);
        }
#elif (GPIO_TEST_MODE == INPUT_TEST)    
        for(int i=0;i<sizeof(gpio_mux);i++)
        {
            // int i=0;
            // gpio_mux[i]=0;
            gpio_config_t io_conf = {};
            //interrupt of rising edge
            io_conf.intr_type = GPIO_INTR_POSEDGE;
            //bit mask of the pins, use GPIO4/5 here
            io_conf.pin_bit_mask = 1ULL<<gpio_mux[i];
            //set as input mode
            io_conf.mode = GPIO_MODE_INPUT;
            //enable pull-up mode
            io_conf.pull_up_en = 1;
            gpio_config(&io_conf);

            //change gpio intrrupt type for one pin
            gpio_set_intr_type(gpio_mux[i], GPIO_INTR_ANYEDGE);

            //create a queue to handle gpio event from isr
            gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
            //start gpio task
            xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);

            //install gpio isr service
            gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
            //hook isr handler for specific gpio pin
            gpio_isr_handler_add(gpio_mux[i], gpio_isr_handler, (void*) gpio_mux[i]);

            printf("gpio%d input test\r\n",gpio_mux[i]);
            vTaskDelay(5000 / portTICK_RATE_MS);
            //remove isr handler for gpio number.
            gpio_isr_handler_remove(gpio_mux[i]);
            gpio_uninstall_isr_service();
            gpio_reset_pin(gpio_mux[i]);
        }
        
#endif        
    }
}
